package controller;

import pojo.Manage;
import pojo.User;
import service.BookService;
import service.ManageService;
import service.UserService;
import view.ManageDetails;
import view.ManageView;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class ManageDetailsListener implements ActionListener {
    private ManageDetails manageDetails;
    public ManageDetailsListener manageDetailsListener;
    private ManageView manageView;
    public ManageDetailsListener(ManageDetails manageDetails, ManageView manageView){
        this.manageDetails=manageDetails;
        this.manageView=manageView;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("返回")){
            manageDetails.dispose();
        }
        if(e.getActionCommand().equals("删除记录")){
            ManageService manageService=new ManageService();
            int a = JOptionPane.showConfirmDialog(manageDetails, "确认删除记录?", "确认信息", JOptionPane.YES_NO_OPTION);
            if (a == JOptionPane.YES_OPTION) {
                manageService.deleteLend(manageDetails.getTfLend().getText());
                manageView.refresh();
                manageDetails.dispose();
            }
        }
        if(e.getActionCommand().equals("续借")){
            int a = JOptionPane.showConfirmDialog(manageDetails, "归还时间是否延长3个月?", "确认信息", JOptionPane.YES_NO_OPTION);
            if (a == JOptionPane.YES_OPTION) {
                ManageService manageService = new ManageService();
                Manage manage = manageService.searchLend(manageDetails.getTfLend().getText());
                Timestamp t = Timestamp.valueOf(manage.getReturnTime());
                Calendar c = Calendar.getInstance();
                c.setTime(t);
//              c.add(Calendar.DATE, 1);  //  加一  天
                c.add(Calendar.MONTH, 3); //  加三个月
                //c.add(Calendar.YEAR,1);   //  加一  年
                Timestamp time1 = new Timestamp(c.getTimeInMillis());
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定义格式，不显示毫秒
                String str = df.format(time1);
                manage.setReturnTime(str);
                manageService.update(manage);
                manageView.refresh();
                manageDetails.dispose();
            }
        }
    }
}
